package com.echo.http;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.echo.util.LogUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by EchoGe on 2016/4/23.
 */
public class HttpManager {
    private static HttpManager mHttpManager;

    public static HttpManager getInstance(){
        if(mHttpManager==null)
            mHttpManager=new HttpManager();
        return mHttpManager;
    }

    public HttpManager(){
        super();
        mHttpManager=this;
    }

    public interface HttpResponse{
        void onMsg(Object response) throws JSONException;
        void onError();
    }

    private void getJsonObject(String url,final HttpResponse httpResponse){
        JsonObjectRequest request=new JsonObjectRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        try {
                            httpResponse.onMsg(jsonObject);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                LogUtils.e("TAG", volleyError.getMessage());
                httpResponse.onError();
            }
        });
        VolleyRequestManager.addRequest(request,null);
    }

    private void getJsonObject(String url,JSONObject jsonObject,final HttpResponse httpResponse){
        JsonObjectRequest request=new JsonObjectRequest(Request.Method.GET,url,jsonObject,
                new Response.Listener<JSONObject>(){

                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        try{
                            httpResponse.onMsg(jsonObject);
                        }catch(JSONException e){
                            e.printStackTrace();
                        }
                    }
                },new Response.ErrorListener(){

            @Override
            public void onErrorResponse(VolleyError volleyError) {
                LogUtils.e("TAG",volleyError.getMessage());
                httpResponse.onError();
            }
        });
        VolleyRequestManager.addRequest(request,null);
    }

    private void getJsonArray(String url,final HttpResponse httpResponse){
        JsonArrayRequest request=new JsonArrayRequest(url,
                new Response.Listener<JSONArray>(){
                    @Override
                    public void onResponse(JSONArray jsonArray) {
                        try {
                            httpResponse.onMsg(jsonArray);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },new Response.ErrorListener(){

            @Override
            public void onErrorResponse(VolleyError volleyError) {
                httpResponse.onError();
            }
        });
        VolleyRequestManager.addRequest(request,null);
    }

    public void regist(String url,final HttpCallBack httpCallBack){
        getJsonObject(url, new HttpResponse() {
            @Override
            public void onMsg(Object response) throws JSONException {
                httpCallBack.onMsg(response);
            }

            @Override
            public void onError() {
                httpCallBack.onError();
            }
        });
    }

    public void login(String url,final HttpCallBack httpCallBack){
        getJsonObject(url, new HttpResponse() {
            @Override
            public void onMsg(Object response) throws JSONException {
                httpCallBack.onMsg(response);
            }

            @Override
            public void onError() {
                httpCallBack.onError();
            }
        });
    }

    public void upload_data(String url,final HttpCallBack httpCallBack){
        getJsonObject(url, new HttpResponse() {
            @Override
            public void onMsg(Object response) throws JSONException {
                httpCallBack.onMsg(response);
            }

            @Override
            public void onError() {
                httpCallBack.onError();
            }
        });
    }

    public void download_data(String url,final HttpCallBack httpCallBack){
        getJsonArray(url, new HttpResponse() {
            @Override
            public void onMsg(Object response) throws JSONException {
                httpCallBack.onMsg(response);
            }

            @Override
            public void onError() {
                httpCallBack.onError();
            }
        });
    }

    public void get_friend(String url,final HttpCallBack httpCallBack){
        getJsonArray(url, new HttpResponse() {
            @Override
            public void onMsg(Object response) throws JSONException {
                httpCallBack.onMsg(response);
            }

            @Override
            public void onError() {
                httpCallBack.onError();
            }
        });
    }

    public void get_friend_request_number(String url,final HttpCallBack httpCallBack){
        getJsonObject(url, new HttpResponse() {
            @Override
            public void onMsg(Object response) throws JSONException {
                httpCallBack.onMsg(response);
            }

            @Override
            public void onError() {
                httpCallBack.onError();
            }
        });
    }

    public void modify_nickname(String url,final HttpCallBack httpCallBack){
        getJsonObject(url, new HttpResponse() {
            @Override
            public void onMsg(Object response) throws JSONException {
                httpCallBack.onMsg(response);
            }

            @Override
            public void onError() {
                httpCallBack.onError();
            }
        });
    }

    public void modify_password(String url,final HttpCallBack httpCallBack){
        getJsonObject(url, new HttpResponse() {
            @Override
            public void onMsg(Object response) throws JSONException {
                httpCallBack.onMsg(response);
            }

            @Override
            public void onError() {
                httpCallBack.onError();
            }
        });
    }


}
